package com.vikstrom.LTimePictureViewer;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by vikstlu on 22.12.2013.
 */
public class LPicture {

    private File pictureFile;
    //private Bitmap pictureBitmap;
    private String name;
    private String description;

    public LPicture(File picture, String name, String description) {
        this.pictureFile = picture;
       // if (picture.exists()) {
            //pictureBitmap = BitmapFactory.decodeFile(picture.getAbsolutePath());
        //}
        this.name = name;
        this.description = description;
    }

    // get description
    private void updateDescription() {
        description = "";
    }

    public File getPicture() {
        return pictureFile;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public Bitmap decodeSampledBitmap(int reqWidth, int reqHeight) {

        FileInputStream fs=null;
        try {
            fs = new FileInputStream(pictureFile);
        } catch (FileNotFoundException e) {
            //TODO do something intelligent
            e.printStackTrace();
        }
        // First decode with inJustDecodeBounds=true to check dimensions
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        try {
            BitmapFactory.decodeFileDescriptor(fs.getFD(),null, options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            options.inPurgeable = true;
            options.inInputShareable = true;
            return BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fs!=null) {
                try {
                    fs.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    private int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
}
